package xyz.qduwo.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;
import xyz.qduwo.entity.CanvasCreatorEntity;
import xyz.qduwo.entity.CanvasEntity;
import xyz.qduwo.entity.QueryResult;
import xyz.qduwo.util.HibernateUtils;

import java.util.List;

public class CanvasCreatorDao {

    /**
     * 保存画布作者
     * @param canvas
     */
    public static void save(CanvasCreatorEntity canvas) {
        Session session = HibernateUtils.openSession();
        try {
            Transaction tx = session.beginTransaction(); // 开启事务
            session.save(canvas);
            tx.commit(); // 提交事务
        } catch (RuntimeException e) {
            session.getTransaction().rollback(); // 回滚事务
            throw e;
        } finally {
            session.close(); // 关闭session
        }
    }


    /**
     * 更新画布作者
     * @param canvas
     */
    public static void update(CanvasCreatorEntity canvas) {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            session.update(canvas);// 操作

            tx.commit();
        } catch (RuntimeException e) {
            assert tx != null;
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }


    /**
     * 删除画布作者
     * @param canvas
     */
    public static void delete(CanvasCreatorEntity canvas) {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.delete(canvas); // 删除的是实体对象

            tx.commit();
        } catch (RuntimeException e) {
            assert tx != null;
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }


    /**
     * 根据画布对象查询当前画布对应的作者列表
     * @param canvasEntity
     * @return
     */
    public static List listuserimage(CanvasEntity canvasEntity) {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            List list =session.createQuery("FROM CanvasCreatorEntity WHERE canvas='"+canvasEntity.getId()+"'").list(); // 使用HQL查询
            tx.commit();
            return list;
        } catch (RuntimeException e) {
            assert tx != null;
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }

    /**
     * 分页的查询数据列表
     * @param firstResult 从结果列表中的哪个索引开始取数据
     * @param maxResults 最多取多少条数据
     * @return 一页的数据列表
     */
    @SuppressWarnings("unchecked")
    public static QueryResult findAll(int firstResult, int maxResults) {
        Session session = HibernateUtils.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            // 查询一页的数据列表
            // 方式一：
            // Query query = session.createQuery("FROM CanvasEntity");
            // query.setFirstResult(firstResult);
            // query.setMaxResults(maxResults);
            // List<CanvasEntity> list = query.list(); // 使用HQL查询

            // 方式二：方法链
            List<CanvasEntity> list = session.createQuery( //
                    "FROM CanvasEntity") //
                    .setFirstResult(firstResult) //
                    .setMaxResults(maxResults) //
                    .list();

            // 查询总记录数
            // session.createQuery("SELECT COUNT(*) FROM CanvasEntity").list().get(0);
            // Long count = (Long) session.createQuery("SELECT COUNT(*) FROM CanvasEntity").uniqueResult();
            Long count = (Long) session.createQuery( //
                    "SELECT COUNT(*) FROM CanvasEntity") //
                    .uniqueResult();
            tx.commit();

            // 返回结果
            return new QueryResult(count.intValue(), list);
        } catch (RuntimeException e) {
            assert tx != null;
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }
}
